<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/13
 * Time: 17:47
 */

namespace app\api\controller;


use think\Db;

class Activity extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 活动列表
     * @return \think\response\Json
     * type       活动种类    1 点即丽    2 花样青青
     * limit      1 展示3条    2 列表
     * page       页码
     */
    public function activityList(){
        $type = input('type'); // 1 点即丽   2 花样青青
        $limit = input('limit');   // 1 3条   2 列表
        
        $query = Db::table('up_activity')->where('activity_type', $type)->field('activity_id, activity_title, activity_thumb, activity_is_show');
        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_rule')->find();
        if($type == 1){
            if($user['user_rule'] == 1){
                $query->where('activity_is_show', 1);
            }elseif ($user['user_rule'] == 2){
                $query->where('activity_is_show', 'IN', '1,2');
            }elseif ($user['user_rule'] == 3){
                $query->where('activity_is_show', 'IN', '1,3');
            }
        }
        if($limit == 1){
            $query->limit(3);
        }else{
            $query->page($this->page, 10);
        }
        $data = $query->order('activity_sort desc, activity_time desc')->select();
        if($data){
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 活动详情
     * @return \think\response\Json
     * activity_id     活动ID
     */
    public function activityDetail(){
        header("Access-Control-Allow-Origin: *");
        $activity_id = input('activity_id');
        $detail = Db::table('up_activity')
            ->where('activity_id', $activity_id)
            ->field('activity_title, activity_thumb, activity_content, activity_time, activity_type, activity_is_show')
            ->find();
        if($this->user_id != 0){
            $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_rule')->find();
            if($user['user_rule'] == 1 && ($detail['activity_is_show'] == 2 || $detail['activity_is_show'] == 3)){
                return json(['status' => 0, 'msg' => '阅读权限不足']);
            }
            if($user['user_rule'] == 2 && $detail['activity_is_show'] == 3){
                return json(['status' => 0, 'msg' => '阅读权限不足']);
            }
            if($user['user_rule'] == 3 && $detail['activity_is_show'] == 2){
                return json(['status' => 0, 'msg' => '阅读权限不足']);
            }
        }

        return json(['status' => 200, 'msg' => $detail]);
    }
}